import cfg import socket import re import time import multiprocessing import random import math def chat(sock, msg): """ Send a chat message to the server. Keyword arguments: sock -- the socket over which to send the message msg -- the message to be sent """ sock.send("PRIVMSG {} :{}\r\n".format(cfg.CHAN, msg).encode("utf-8")) def ban(sock, user): """ Ban a user from the current channel. Keyword arguments: sock -- the socket over which to send the ban command user -- the user to be banned """ chat(sock, "/ban {}".format(user)) def unban(sock, user): """ Ban a user from the current channel. Keyword arguments: sock -- the socket over which to send the ban command user -- the user to be banned """ chat(sock, "/unban {}".format(user)) def timeout(sock, user, secs): """ Time out a user for a set period of time. Keyword arguments: sock -- the socket over which to send the timeout command user -- the user to be timed out secs -- the length of the timeout in seconds (default 600) """ chat(sock, "/timeout {}".format(user, secs)) def untimeout(sock,user): chat(sock,"/untimeout {}".format(user)) def emoteonly(sock): """ Ban a user from the current channel. Keyword arguments: sock -- the socket over which to send the ban command user -- the user to be banned """ chat(sock, "/emoteonly {}") def emoteonly(sock): """ Ban a user from the current channel. Keyword arguments: sock -- the socket over which to send the ban command user -- the user to be banned """ chat(sock, "/emoteonlyoff {}") s = socket.socket() s.connect((cfg.HOST,cfg.PORT)) s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8")) s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8")) s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8")) CHAT_MSG=re.compile(r"^:\w+!\w+@\w+.tmi.twitch.tv PRIVMSG #\w+ :") vips = [] vrem = [] vipc = [] vipCount = 0 while True: response = s.recv(1024).decode("utf-8") if response == "PING :tmi.twitch.tv\r\n": s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) print('sent') else: username = re.search(r"\w+", response).group(0) # return the entire match message = CHAT_MSG.sub("", response) # s.send(s,username +" + " + message) print(username + ": " + message) if username in vips: # will need to edit out tvheadbot, atltvhead, tmi from the messages collected! before doing the thanos glove # if its tvhead bot do nothing ind = vips.index(username) vipCount = vipc[ind] vipCount = vipCount + 1 vipc[ind] = vipCount print(vips) print(vipc) # print(username + " has spoken " + str(vipCount) + " times.") else: vips.append(username) vipc.append(1) if message.strip() == "SNAP" and username == "tvheadbot": # these operations move half of the chat to a new list for removal oglength = len(vips) delenght = math.ceil((oglength-3)/2) while len(vrem) < delenght: userToMove = random.choice(vips) if userToMove == "tmi" or userToMove == "tvheadbot" or userToMove == "atltvhead": # do nothing print("Encountered either Tmi or tvheadbot or atltvhead") else: if userToMove in vrem: print("User " + userToMove + " is already to be timed out.") else: vrem.append(userToMove) # this is where I do the timeout for q in vrem: # print(q) timeout(s, q, 10) time.sleep(1 / cfg.RATE) # remember to wipe out vrem I want to unban / untimout time.sleep(1 / cfg.RATE)